package com.ruoyi.exam.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.exam.vo.ExamQuestionCategoryVo;
import com.ruoyi.exam.service.IExamQuestionCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 题库分类Controller
 *
 * @author Tellsea
 * @date 2025-05-30
 */
@Api(tags = "题库分类Controller")
@RestController
@RequestMapping("/exam/questionCategory")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class ExamQuestionCategoryController extends BaseController {

    private final IExamQuestionCategoryService examQuestionCategoryService;

    @ApiOperation("查询题库分类列表")
    @PreAuthorize("@ss.hasPermi('exam:questionCategory:list')")
    @GetMapping("/list")
    public TableDataInfo<ExamQuestionCategoryVo> list(ExamQuestionCategoryVo entity) {
        return examQuestionCategoryService.queryList(entity);
    }

    @ApiOperation("查询题库分类所有列表")
    @GetMapping("/listAll")
    public AjaxResult listAll(ExamQuestionCategoryVo entity) {
        return AjaxResult.success("查询成功", examQuestionCategoryService.queryAll(entity));
    }

    @ApiOperation("导出题库分类列表")
    @PreAuthorize("@ss.hasPermi('exam:questionCategory:export')")
    @Log(title = "题库分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ExamQuestionCategoryVo entity) {
        List<ExamQuestionCategoryVo> list = examQuestionCategoryService.queryAll(entity);
        ExcelUtil<ExamQuestionCategoryVo> util = new ExcelUtil<>(ExamQuestionCategoryVo.class);
        util.exportExcel(response, list, "题库分类数据");
    }

    @ApiOperation("获取题库分类详细信息")
    @PreAuthorize("@ss.hasPermi('exam:questionCategory:query')")
    @GetMapping(value = "/getInfo/{categoryId}")
    public AjaxResult getInfo(@PathVariable("categoryId") Long categoryId) {
        return AjaxResult.success("查询成功", examQuestionCategoryService.queryById(categoryId));
    }

    @ApiOperation("新增题库分类")
    @PreAuthorize("@ss.hasPermi('exam:questionCategory:add')")
    @Log(title = "题库分类", businessType = BusinessType.INSERT)
    @PostMapping("add")
    public AjaxResult add(@RequestBody ExamQuestionCategoryVo entity) {
        return toAjax(examQuestionCategoryService.save(entity));
    }

    @ApiOperation("修改题库分类")
    @PreAuthorize("@ss.hasPermi('exam:questionCategory:edit')")
    @Log(title = "题库分类", businessType = BusinessType.UPDATE)
    @PostMapping("edit")
    public AjaxResult edit(@RequestBody ExamQuestionCategoryVo entity) {
        return toAjax(examQuestionCategoryService.updateById(entity));
    }

    @ApiOperation("删除题库分类")
    @PreAuthorize("@ss.hasPermi('exam:questionCategory:remove')")
    @Log(title = "题库分类", businessType = BusinessType.DELETE)
	@GetMapping("/remove/{categoryIds}")
    public AjaxResult remove(@PathVariable Long[] categoryIds) {
        return toAjax(examQuestionCategoryService.removeByIds(Arrays.asList(categoryIds)) ? 1 : 0);
    }
}
